import requests
from lxml import etree
import time

class Image(object):
    def __init__(self):
        self.url ='https://image.baidu.com/search/acjson?'
        self.headers={
            'Cookie': 'BDqhfp=%E5%A3%81%E7%BA%B8%26%2600-10-1undefined%26%260%26%261; BIDUPSID=B153741D04D5916F54ED1502D8D5C8EF; PSTM=1622556085; BAIDUID=B153741D04D5916FEACA3EFFABF5682E:FG=1; BAIDUID_BFESS=B153741D04D5916F54ED1502D8D5C8EF:FG=1; BDRCVFR[-Cxg3mV_4Yc]=LpDQ8KNdUpDUhGBrHT8mvqV; H_PS_PSSID=31254_26350; BA_HECTOR=0g0h0lagah8h8g8l2c1gbkemt0r; BDRCVFR[X_XKQks0S63]=mk3SLVN4HKm; userFrom=www.baidu.com; __yjs_duid=1_9d7f63cdd1e7f6198bf18ace7bf03a581622817497565; firstShowTip=1; BDRCVFR[dG2JNJb_ajR]=mk3SLVN4HKm; BDRCVFR[-pGxjrCMryR]=mk3SLVN4HKm; ab_sr=1.0.0_ZTAyYTVmMjJhNzhkYWJhZTk5ODVlYzY4OTdjYTRjMjcxZTY1ZDliZWU4Y2Y1NGQ0M2RhOTZmOTVhODNkYzBkZjBiOThmZWU3NGRiN2RmZTc4NmIwNmU1Mjc1NzQzNWJm',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36'    
        }
        self.params = {
            'tn': 'resultjson_com',
            'logid': '10774211529936105179',
            'ipn': 'rj',
            'ct': '201326592',
            'is':'',
            'fp': 'result',
            'queryWord': '壁纸',
            'cl': '2',
            'lm': '-1',
            'ie': 'utf-8',
            'oe': 'utf-8',
            'adpicid':'', 
            'st': '-1',
            'z': '0',
            'ic': '0',
            'hd': '0',
            'latest': '0',
            'copyright': '0',
            'word': '壁纸',
            's':'',
            'se':'' ,
            'tab':'',
            'width':'', 
            'height':'', 
            'face': '0',
            'istype': '2',
            'qc':'', 
            'nc': '1',
            'fr':'', 
            'expermode':'', 
            'nojc':'', 
            'cg': 'wallpaper',
            'pn': '60',
            'rn': '30',
            'gsm': '',
            'time':'' 
        }
        self.image_list = []

    def get_image(self,num):
        for i in range(0,num):
            self.params['time'] = int(time.time() * 1000)
            self.params['pn']=i * 30
            response = requests.get(url=self.url,headers=self.headers,params=self.params)
            for j in range(0,len(response.json()['data'])-1):
                self.image_list.append(response.json()['data'][j]['thumbURL'])

    def save_image(self):
        n=1
        for i in self.image_list:
            image = requests.get(url=i)
            with open('D:\爬虫图片\{}.jpg'.format(n),'wb') as file:
                file.write(image.content)
            n+=1

if __name__ == '__main__':
    image = Image()
    image.get_image(2)
    image.save_image()